# #!/usr/bin/python
import json
import requests
#掘金_lishikai


# 文章发布
def juejin_post_publish(response,category,label_array_push):
    content = response.css('.article-content').extract()[0].replace('data-src', 'data-original')
    # 关注验证
    check = '<div class="asb-post-01"> <div class="mask"></div> <div class="info"> <div>扫码或搜索：<span style="color: #E9405A; font-weight: bold;">白话java框架源码</span></div> <div> <span>发送 </span><span class="token" style="color: #e9415a; font-weight: bold; font-size: 17px; margin-bottom: 45px;">more</span> </div> <div> 即可<span style="color: #e9415a; font-weight: bold;">获取验证码立即永久</span>解锁本站全部文章 </div> <div> <img class="code-img" style="width: 300px;display:unset" src="https://gitee.com/li521miao/xsj_lsk/raw/master/xsj/1599990462701.png"> </div> <div> <div>输入验证码：<input id="code" type="text" value="" /></div> <div class="qr" >确认</div> </div> </div> </div>'
    total_content = content + check + "<br/>来自：" + response._url;
    print(total_content)
    # 声明来自哪儿
    # category = [8]  # 1 | java基础源码研究   2 | 最近文章    3 | linux   4 | 数据库  5 | 微信收藏     6 | 代码实验室    7 | 面试官  8 | 开发日常
    title = response.css('title::text').extract_first()
    # wp api发送文章到对应
    # 往wordpress新增该文章
    import requests
    data = {
        'title': title,
        'content': total_content,
        'comment_status': 'open',
        'ping_status': 'open',
        'status': 'publish',
        'categories': category,
        'tags':label_array_push

    }
    response = requests.post('http://114.215.210.34/wp-json/wp/v2/posts', data=data, auth=('lsk', '1992A1573'))
    return response
# 文章更新
def juejin_post_update(response,id,category,label_array_push):
    content = response.css('.article-content').extract()[0].replace('data-src', 'data-original')
    # 关注验证
    check = '<div class="asb-post-01"> <div class="mask"></div> <div class="info"> <div>扫码或搜索：<span style="color: #E9405A; font-weight: bold;">白话java框架源码</span></div> <div> <span>发送 </span><span class="token" style="color: #e9415a; font-weight: bold; font-size: 17px; margin-bottom: 45px;">more</span> </div> <div> 即可<span style="color: #e9415a; font-weight: bold;">获取验证码立即永久</span>解锁本站全部文章 </div> <div> <img class="code-img" style="width: 300px;display:unset" src="https://gitee.com/li521miao/xsj_lsk/raw/master/xsj/1599990462701.png"> </div> <div> <div>输入验证码：<input id="code" type="text" value="" /></div> <div class="qr" >确认</div> </div> </div> </div>'
    total_content = content + check + "<br/>来自：" + response._url;
    print(total_content)
    # 声明来自哪儿
    # category = [8]  # 1 | java基础源码研究   2 | 最近文章    3 | linux   4 | 数据库  5 | 微信收藏     6 | 代码实验室    7 | 面试官  8 | 开发日常
    title = response.css('title::text').extract_first()
    # wp api发送文章到对应
    # 往wordpress新增该文章
    import requests
    data = {
        'title': title,
        'content': total_content,
        'categories': category,
        'tags': label_array_push
    }
    response = requests.post('http://114.215.210.34/wp-json/wp/v2/posts/'+id, data=data, auth=('lsk', '1992A1573'))
    return response



#
def test_wp_category_add(category):
    headers = {
        'accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
    }
    data = {
        'name': category
    }

    response = requests.post('http://blog.memlee.top/wp-json/wp/v2/categories', headers=headers, data=data, auth=('lsk', '1992A1573'))
    jsonreturn = json.loads(response.text.replace('\n', '').replace('\r', ''))
    return jsonreturn




#
def test_wp_tag_add(tag):
    import requests

    headers = {
        'accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
    }

    data = {
        'name': tag
    }

    response = requests.post('http://blog.memlee.top/wp-json/wp/v2/tags', headers=headers, data=data, auth=('lsk', '1992A1573'))
    jsonreturn = json.loads(response.text.replace('\n', '').replace('\r', ''))
    return jsonreturn







# 获取分类
def wp_api_getallCategories(page=None, per_page=None):
    import json
    headers = {
        'accept': 'application/json',
    }
    response = requests.get('http://blog.memlee.top/wp-json/wp/v2/categories', headers=headers)
    json1 = json.loads(response.text.replace('\n', '').replace('\r', ''))
    print(json1)

    total_categories = {}
    for json in json1:
        total_categories[json['name']]= json['id']

    return total_categories

def test_juejin_api():
    import requests

    data = {
        'title': 'New Title11'
    }

    response = requests.post('http://114.215.210.34/wp-json/wp/v2/posts/173', data=data, auth=('lsk', '1992A1573'))

# 获取掘金的本页限定数量的文章，并返回总页数
def test_juejin_api_getallPosts(page=None,per_page=None):

    headers = {
        'accept': 'application/json',
    }

    params = (
        ('page', page),
        ('per_page', per_page),
        ('order', 'desc'),
    )

    response = requests.get('http://114.215.210.34/wp-json/wp/v2/posts', headers=headers, params=params)
    pages = response.headers._store['x-wp-totalpages'][1]
    json1 = json.loads(response.text.replace('\n', '').replace('\r', ''))
    print(json1)
    print(pages)
    return {"pages":pages,"page_data":json1}















# if __name__ == '__main__':
    # test_juejin_api()